package cn.school.controller.stu.evaluation;

import cn.school.classManage.vo.ClassCurriculumDetailVO;
import cn.school.classManage.vo.ClassCurriculumExportVO;
import cn.school.classManage.vo.ClassCurriculumImportVO;
import cn.school.classManage.vo.ClassCurriculumMainVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.stu.evaluation.entity.StuEvaData;
import cn.school.stu.evaluation.service.StuEvaDataService;
import cn.school.stu.evaluation.service.StuEvaTaskService;
import cn.school.stu.evaluation.vo.StuEvaDataExportVO;
import cn.school.student.entity.StudentInfo;
import cn.school.student.vo.ExamResultPOI;
import cn.school.workflow.service.ViolativeApplyService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/5/26 10:10
 * @description:
 */
@RestController
@Api(tags = "StuEvaDataController => 学生考评数据控制器")
@RequestMapping("/stu/evaluation/data")
public class StuEvaDataController {

    @Autowired
    StuEvaDataService stuEvaDataService;
    @Autowired
    private ViolativeApplyService violativeApplyService;

    @PostMapping("/createStuEvaData")
    public CommonResult createStuEvaData(@RequestBody StuEvaData stuEvaData){
        int num = stuEvaDataService.createStuEvaData(stuEvaData);
        if(num > 0) {
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 提交学生考评分
     */
    @PostMapping("/createStuEvaDataBatch")
    public CommonResult createStuEvaDataBatch(@RequestBody List<StuEvaData> stuEvaDataList){
        boolean flag = stuEvaDataService.createStuEvaDataBatch(stuEvaDataList);
        if(flag){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/findStuEvaDataByStuId/{stuId}")
    public CommonResult findStuEvaDataByStuId(@PathVariable("stuId")Long stuId){
        List<StuEvaData> stuEvaData = stuEvaDataService.findStuEvaDataByStuId(stuId);
        return CommonResult.success(stuEvaData);
    }

    @GetMapping("/findStuEvaDataByTaskId/{taskId}")
    public CommonResult findStuEvaDataByTaskId(@PathVariable("taskId")Long taskId){
        List<StuEvaData> stuEvaData = stuEvaDataService.findStuEvaDataByTaskId(taskId);
        return CommonResult.success(stuEvaData);
    }

    @ApiOperation("findStuEvaDataPageList => 分页查询")
    @PostMapping("/findStuEvaDataPageList")
    public CommonResult<CommonPage<StuEvaData>> findStuEvaDataPageList(@RequestBody StuEvaData vo){
        IPage<StuEvaData> pageList = stuEvaDataService.findStuEvaDataPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("导出模板")
    @GetMapping("/exportTemplate/{planId}/{classId}/{taskId}/{remk}/{planName}")
    public CommonResult export(HttpServletResponse response, @PathVariable("planId")Long planId,
                               @PathVariable("classId")String classId,@PathVariable("taskId")String taskId, @PathVariable("remk")String remk, @PathVariable("planName")String planName) throws IOException {
        List<StuEvaDataExportVO> allList = new ArrayList<>();
        //获取班级学生信息
        List<StudentInfo> studentList = violativeApplyService.findStudentByClassId(classId);
        StuEvaDataExportVO exportVO = null;
        for (StudentInfo studentInfo : studentList) {
            exportVO = new StuEvaDataExportVO();
            exportVO.setStudentId(studentInfo.getId()+"");
            exportVO.setStudentName(studentInfo.getName());
            exportVO.setClassName(studentInfo.getClassName());
            exportVO.setTaskId(taskId);
            exportVO.setClassId(classId);
            exportVO.setTermTimeName(remk);
            exportVO.setEvaPlanName(planName);
            allList.add(exportVO);

        }

        EasyExcel.write(response.getOutputStream(), null).head(getHeader(planId, classId,taskId,remk)).sheet("班级课表").doWrite(allList);
        return CommonResult.failed();
    }

    @ApiOperation("导入数据")
    @PostMapping("/import")
    public CommonResult<List<StuEvaData>> importExcel(@RequestParam("upload") MultipartFile multipartFile) throws IOException {
        return stuEvaDataService.importExcel(multipartFile.getInputStream());
    }

    @Resource
    StuEvaTaskService stuEvaTaskService;
    private List<List<String>> getHeader(Long planId,String classId,String taskId, String remk){
        List<List<String>> handList = new ArrayList<>();
        //获取指标内容

        List<Map<String, Object>> stuEvaData = stuEvaTaskService.findStuEvaScoreTemplateByPlanId(planId);
        List<String> taskList = new ArrayList<>();
        taskList.add("任务ID");
        taskList.add("任务ID");
        handList.add(taskList);
        List<String> pandList = new ArrayList<>();
        pandList.add("任务名称");
        pandList.add("任务名称");
        handList.add(pandList);
        List<String> remkList = new ArrayList<>();
        remkList.add("学年学期");
        remkList.add("学年学期");
        handList.add(remkList);
        List<String> classIdList = new ArrayList<>();
        classIdList.add("班级ID");
        classIdList.add("班级ID");
        handList.add(classIdList);
        List<String> studentNameList = new ArrayList<>();
        studentNameList.add("学生姓名");
        studentNameList.add("学生姓名");
        handList.add(studentNameList);
        List<String> classnameList = new ArrayList<>();
        classnameList.add("班级");
        classnameList.add("班级");
        handList.add(classnameList);
        List<String> studentIdList = new ArrayList<>();
        studentIdList.add("学生编号");
        studentIdList.add("学生编号");
        handList.add(studentIdList);

        if(stuEvaData!=null && stuEvaData.size()>0){
            List<String> dongtaiList = new ArrayList<>();
            for (Map<String, Object> data : stuEvaData) {
                dongtaiList.add(String.valueOf(data.get("indicator")));
                dongtaiList.add(String.valueOf(data.get("element")));
                handList.add(dongtaiList);
                dongtaiList = new ArrayList<>();
            }
        }
        return handList;
    }
}
